

package com.viweb.storemanager.core;

import javax.swing.JOptionPane;


public class OptionPane {
    
    private static String strThongBao = "Th\u00F4ng b\u00E1o";
    
    public static final byte MsgDEFAULT_BUTTON1 = 0;
    public static final byte MsgDEFAULT_BUTTON2 = 1;
    public static final byte MsgDEFAULT_BUTTON3 = 2;
    
    public static byte m_DialogResult;
    public static final byte MsgRESULT_CLOSED = 0;
    public static final byte MsgRESULT_YES = 1;
    public static final byte MsgRESULT_NO = 2;
    public static final byte MsgRESULT_CANCEL = 3;
    public static final byte MsgRESULT_RETRY = 4;
    public static final byte MsgRESULT_ABOUT = 5;
    
    public static final byte MsgTYPE_INFO = 6;
    public static final byte MsgTYPE_WARNING = 7;
    public static final byte MsgTYPE_ERROR = 8;
    public static final byte MsgTYPE_YESNO = 9;
    public static final byte MsgTYPE_YESNOCANCEL = 10;
    public static final byte MsgTYPE_RETRYCANCEL = 11;
    public static final byte MsgTYPE_ABORTRETRYCANCEL = 12;
    
    public static byte conVertTypeResult(int iResult, byte MsgTYPE_) {
        byte bRS = 0;
        switch(iResult){
            //Khi dong Dialog bang cach CLICK NUT Close o tren goc tren-phai
            case JOptionPane.CLOSED_OPTION:
                bRS = MsgRESULT_CLOSED;
                break;
                
                //Gia tri tra ve cua ham JOptionPane.showOptionDialog(...) la CANCEL_OPTION
            case JOptionPane.CANCEL_OPTION:
                bRS = MsgRESULT_CANCEL;
                break;
                
                //Gia tri tra ve cua ham JOptionPane.showOptionDialog(...) la YES_OPTION
            case JOptionPane.YES_OPTION://chinh =JOptionPane.OK_OPTION
                //Chon button "Thu lai"
                if(MsgTYPE_ == MsgTYPE_RETRYCANCEL)
                    bRS = MsgRESULT_RETRY;
                //Chon button "Thoat"
                else if(MsgTYPE_ == MsgTYPE_ABORTRETRYCANCEL)
                    bRS = MsgRESULT_ABOUT;
                //Chon button "Dong y" hoac	click button "Dong" trong cac OptionPane chi co 1 button
                else //if(MsgTYPE_ == MsgTYPE_YESNO || MsgTYPE_ == MsgTYPE_YESNOCANCEL)
                    bRS = MsgRESULT_YES;
                break;
                
                //Gia tri tra ve cua ham JOptionPane.showOptionDialog(...) la NO_OPTION
            case JOptionPane.NO_OPTION:
                if(MsgTYPE_ == MsgTYPE_YESNO || MsgTYPE_ == MsgTYPE_YESNOCANCEL)
                    bRS = MsgRESULT_NO;
                else if(MsgTYPE_ == MsgTYPE_ABORTRETRYCANCEL)
                    bRS = MsgRESULT_RETRY;
                else if(MsgTYPE_ == MsgTYPE_RETRYCANCEL)
                    bRS = MsgRESULT_CANCEL;
                break;
        };
        
        return bRS;
    }
    
    public static byte show(java.awt.Frame parent, String strMsg) {
        String[] option = {"   \u0110\u00F3ng   "};
        m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strThongBao, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option, option[0]), OptionPane.MsgTYPE_INFO);
        return m_DialogResult;
    }
    
    public static byte show(java.awt.Frame parent, String strMsg, String strTitle) {
        String[]	option = {"   \u0110\u00F3ng   "};
        m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option, option[0]), OptionPane.MsgTYPE_INFO);
        return m_DialogResult;
    }
    
    public static byte show(java.awt.Frame parent, String strMsg, String strTitle, byte MsgTYPE_) {
        return show(parent, strMsg, strTitle, MsgTYPE_, MsgDEFAULT_BUTTON1 );
    }
    
    public static byte show(java.awt.Frame parent, String strMsg, String strTitle, byte MsgTYPE_, byte MsgDEFAULT_) {
        //Set value to option1
        String[]	option1 = {"   \u0110\u00F3ng   "};
        //Set type of MsgBoxEx
        switch(MsgTYPE_) {
            case 6: //MsgTYPE_INFO:
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option1, option1[0]), MsgTYPE_);
                break;
            case 7: //MsgTYPE_WARNING:
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, option1, option1[0]), MsgTYPE_);
                break;
            case 8: //MsgTYPE_ERROR:
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, option1, option1[0]), MsgTYPE_);
                break;
            case 9: //MsgTYPE_YESNO:
                String[]	option2 = {"   \u0110\u1ED3ng \u00FD   ", "Kh\u00F4ng"};
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option2, option2[MsgDEFAULT_]), MsgTYPE_);
                break;
            case 10: //MsgTYPE_YESNOCANCEL:
                String[]	option3 = {"   \u0110\u1ED3ng \u00FD   ", "Kh\u00F4ng", "H\u1EE7y b\u1ECF"};
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, option3, option3[MsgDEFAULT_]), MsgTYPE_);
                break;
            case 11: //MsgTYPE_RETRYCANCEL:
                String[]	option4 = {"   Th\u1EED l\u1EA1i   ", "H\u1EE7y b\u1ECF"};
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, option4, option4[MsgDEFAULT_]), MsgTYPE_);
                break;
            case 12: //MsgTYPE_ABORTRETRYCANCEL:
                String[]	option5 = {"Tho\u00E1t", "   Th\u1EED l\u1EA1i   ", "H\u1EE7y b\u1ECF"};
                m_DialogResult = conVertTypeResult(JOptionPaneEx.showOptionDialog(parent, strMsg, strTitle, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, option5, option5[MsgDEFAULT_]), MsgTYPE_);
                break;
        };
        
        return m_DialogResult;
    }
    
    class JOptionPaneEx extends JOptionPane implements java.awt.event.KeyListener{
//        public void setLocation(int x, int y) {
//            // Centering the dialog
//            Dimension d = this.getSize();
//            Dimension screenSize = this.getParent().getSize();
//            this.setBounds(
//                    (int)(0.5 * (screenSize.width - d.getWidth())),
//                    (int)(0.5 * (screenSize.height - d.getHeight())),
//                    d.width, d.height);
//        }
        
        public void keyPressed(java.awt.event.KeyEvent e) {
            //javax.swing.JOptionPane.showConfirmDialog(null, "Xay ra su kien KeyDown!");
            if( e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER )
                e.setKeyCode(java.awt.event.KeyEvent.VK_SPACE);
            super.processKeyEvent(e);
        }
        
        public void keyReleased(java.awt.event.KeyEvent e) {
            super.processKeyEvent(e);
        }
        
        public void keyTyped(java.awt.event.KeyEvent e) {
            super.processKeyEvent(e);
        }
        
    }
}
